#include "AdventureCardArea.h"
#include "AdventureCard.h"
#include "AdventureCardFactory.h"
#include <fstream>
#include <qevent.h>
#include <qpainter.h>
#include <qfiledialog.h>

AdventureCardArea::AdventureCardArea(QWidget *parent) : 
QWidget(parent)
,mFaceUpStack(0)
,mFaceDownStack(0)
,layout(0)
,clickToLoad(0)
{
	setFixedSize(350, 240);

	// Create a label that says "click here to load the deck"
	layout = new QHBoxLayout(this);
	clickToLoad.setText("Click here to load the adventure card deck from file!");
	clickToLoad.setWordWrap(true);
	clickToLoad.setMaximumWidth(350);

	// Set font to be a bit larger and align at the center
	QFont f = clickToLoad.font();
	f.setPointSize(10);
	clickToLoad.setFont(f);
	clickToLoad.setStyleSheet("QLabel { qproperty-alignment: AlignCenter; }");

	positionCards();
}

// Destructor
AdventureCardArea::~AdventureCardArea()
{
	if (0 != mFaceUpStack)
	{
		for (int i = 0; i < mFaceUpStack->size(); ++i)
		{
			if (0 != mFaceUpStack->at(i))
				delete mFaceUpStack->at(i);
		}
		mFaceUpStack->clear();
		delete mFaceUpStack;
	}
	if (0 != mFaceDownStack)
	{
		for (int i = 0; i < mFaceDownStack->size(); ++i)
		{
			if (0 != mFaceDownStack->at(i))
				delete mFaceDownStack->at(i);
		}
		mFaceDownStack->clear();
		delete mFaceDownStack;
	}

	if(0 != layout)
		delete layout;
}

// Flips the card to the next and puts the active card onto the face-down part of the deck
// If all cards are used up, callse restart()
void AdventureCardArea::nextCard(void)
{
	
	if(0 != mFaceDownStack && mFaceDownStack->size() > 0)
		mFaceDownStack->back()->setVisible(false);

	if(0 != mFaceUpStack && mFaceUpStack->size() > 0)
		mFaceUpStack->back()->setVisible(false);

	if(0 != mFaceDownStack && 0 != mFaceUpStack && mFaceUpStack->size() > 0)
	{
		AdventureCard * card = mFaceUpStack->back();
		mFaceUpStack->pop_back();
		card->setFaceUp(false);
		mFaceDownStack->push_back(card);
	}
	else
	{
		restart();
	}

	positionCards();
}

// Moves all cards from face-down to face-up stack and turns them face up
void AdventureCardArea::restart(void)
{
	if(0 != mFaceDownStack && 0 != mFaceUpStack)
	{
		for (int i = mFaceDownStack->size()-1; i >= 0; --i)
		{
			AdventureCard * card = mFaceDownStack->at(i);
			card->setFaceUp(true);
			mFaceUpStack->push_back(card);
		}
		mFaceDownStack->clear();
	}
}

// Loads the deck from file
void AdventureCardArea::load(string iFileName)
{
	// Clear the existing cards
	if (0 != mFaceUpStack)
	{
		for (int i = 0; i < mFaceUpStack->size(); ++i)
		{
			if (0 != mFaceUpStack->at(i))
				delete mFaceUpStack->at(i);
		}
		mFaceUpStack->clear();
	}
	if (0 != mFaceDownStack)
	{
		for (int i = 0; i < mFaceDownStack->size(); ++i)
		{
			if (0 != mFaceDownStack->at(i))
				delete mFaceDownStack->at(i);
		}
		mFaceDownStack->clear();
	}

	mFaceUpStack = AdventureCardFactory::loadFromDeck(iFileName);

	if (0 == mFaceDownStack)
		mFaceDownStack = new vector<AdventureCard *>();
	else
		mFaceDownStack->clear();

	positionCards();
}

// Arranges the widgets on the layout so that the active card is on the left, and the face down cards (if any) - on the right
void AdventureCardArea::positionCards(void)
{
	if (mFaceUpStack == 0 || mFaceDownStack ==0)
	{
		layout->addWidget(&clickToLoad);
		clickToLoad.setVisible(true);
	}
	else
	{
		clickToLoad.setVisible(false);
	}
	if(0 != mFaceUpStack && mFaceUpStack->size() > 0)
	{
		layout->addWidget(mFaceUpStack->back());
		mFaceUpStack->back()->setVisible(true);
	}
	if(0 != mFaceDownStack && mFaceDownStack->size() > 0)
	{
		layout->addWidget(mFaceDownStack->back());
		mFaceDownStack->back()->setVisible(true);
	}
}

AdventureCard * AdventureCardArea::getActiveAdventureCard(void) const
{
	if (0 != mFaceUpStack
		&& mFaceUpStack->size() > 0)
		return mFaceUpStack->back();
	else
		return 0;
}

// Handles mouse clicks - flips cards or asks the user to load the deck
 void AdventureCardArea::mousePressEvent(QMouseEvent *event)
 {
     if (event->button() == Qt::LeftButton) 
	 {
		 if (mFaceUpStack != 0 && mFaceDownStack != 0)
		 {
			nextCard();
		 }
		 else
		 {
			QString qfileName = QFileDialog::getOpenFileName(this, tr("Open File"), "", tr("Deck files (*.deck)"));

			if(qfileName.size() == 0)
				return;

			load(qfileName.toStdString());
		 }
     }
	 
 }